package org.minuframework.encrypt;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class UrlUtils {
    
    // ------------------------------------------------------------------------
    // ************************************************************************
    // constants
    
    /**
     * 字符编码 utf-8
     */
    private static final String UTF8 = "UTF-8";
    
    // ************************************************************************
    // public methods
    
    /**
     * @param  str                          文本内容 String
     * @return                              String
     * @throws UnsupportedEncodingException
     */
    public static String urlEncoder(String str) throws UnsupportedEncodingException {
        return URLEncoder.encode(str, UTF8);
    }
    
    /**
     * @param  str                          文本内容 String
     * @param  charset                      字符编码 String
     * @return                              String
     * @throws UnsupportedEncodingException
     */
    public static String urlEncoder(String str, String charset) throws UnsupportedEncodingException {
        return URLEncoder.encode(str, charset);
    }
    
    // ------------------------------------------------------------------------
    
    /**
     * @param  str                          文本内容 String
     * @return                              String
     * @throws UnsupportedEncodingException
     */
    public static String urlDencoder(String str) throws UnsupportedEncodingException {
        return URLDecoder.decode(str, UTF8);
    }
    
    /**
     * @param  str                          文本内容 String
     * @param  charset                      字符编码 String
     * @return                              String
     * @throws UnsupportedEncodingException
     */
    public static String urlDencoder(String str, String charset) throws UnsupportedEncodingException {
        return URLDecoder.decode(str, charset);
    }
    
}
